package sudoku.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class sudokuView extends Activity {
	private static final int SUDOKU_SIZE = 9;
	private static int square_WIDTH;
	private static int square_HEIGHT;

	private static final int PADDING = 1;

	private static Sudoku sudoku = new Sudoku(SUDOKU_SIZE);
	private int active_x = 0;
	private int active_y = 0;
	
	private boolean helpNumbers = true;

	private AlertDialog activeDialog;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		DisplayMetrics display = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(display);
		square_WIDTH = (int) Math.floor(display.widthPixels/11);
		square_HEIGHT = square_WIDTH+2;

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		enterSudoku();
		drawSudoku();

	}

	private void enterSudoku() {
		sudoku.setNumber(1, 3, 0);
		sudoku.setNumber(5, 5, 0);
		sudoku.setNumber(1, 0, 1);
		sudoku.setNumber(4, 1, 1);
		sudoku.setNumber(6, 6, 1);
		sudoku.setNumber(7, 7, 1);
		sudoku.setNumber(8, 1, 2);
		sudoku.setNumber(2, 5, 2);
		sudoku.setNumber(4, 6, 2);
		sudoku.setNumber(6, 1, 3);
		sudoku.setNumber(3, 2, 3);
		sudoku.setNumber(7, 4, 3);
		sudoku.setNumber(1, 7, 3);
		sudoku.setNumber(9, 0, 4);
		sudoku.setNumber(3, 8, 4);
		sudoku.setNumber(1, 1, 5);
		sudoku.setNumber(9, 4, 5);
		sudoku.setNumber(5, 6, 5);
		sudoku.setNumber(2, 7, 5);
		sudoku.setNumber(7, 2, 6);
		sudoku.setNumber(2, 3, 6);
		sudoku.setNumber(8, 7, 6);
		sudoku.setNumber(2, 1, 7);
		sudoku.setNumber(6, 2, 7);
		sudoku.setNumber(3, 7, 7);
		sudoku.setNumber(5, 8, 7);
		sudoku.setNumber(4, 3, 8);
		sudoku.setNumber(9, 5, 8);
	}
	

	private void setValue(int number) {
		sudoku.setNumber(number, active_x, active_y);
	}

	private void drawSudoku() {
		LinearLayout layout = (LinearLayout)findViewById(R.sudoku.layout);
		layout.removeAllViews();
		TableLayout table = new TableLayout(this);
		table.setPadding(0, 0, 0, 0);
		layout.addView(table);
		for(int y=0; y<SUDOKU_SIZE; y++) {
			TableRow row = new TableRow(this);
			row.setPadding(PADDING, PADDING, PADDING, PADDING);
			if(y == 3 || y == 6 ) {
				row.setPadding(PADDING, 4, PADDING, PADDING);
			}
			row.setBackgroundColor(Color.BLACK);
			for(int x=0; x<SUDOKU_SIZE; x++) {
				if(x == 3 || x == 6 ) {
					TextView padding = new TextView(this);
					padding.setWidth(4);
					padding.setTextAppearance(this, R.style.buttonText);
					row.addView(padding);
				}
				else {
					TextView padding = new TextView(this);
					padding.setWidth(PADDING);
					padding.setTextAppearance(this, R.style.buttonText);
					row.addView(padding);
				}
				TextView square = new TextView(this);
				square.setPadding(0, 0, 0, 0);
				
				if(sudoku.sudoku[y][x].toString().length() != 1) {
					if(!helpNumbers) {
						square.setText("?");
					}
					else {
						square.setText(sudoku.sudoku[y][x].toString());
						square.setTextAppearance(this, R.style.buttonText);
					}
				}
				else {
					square.setText(sudoku.sudoku[y][x].toString());
					square.setTextAppearance(this, R.style.bigButtonText);
				}
				
				if(sudoku.sudoku[y][x].howManyPossibilities() < 1 && !sudoku.sudoku[y][x].isSure()) {
					square.setBackgroundColor(Color.RED);
				}
				else {
					square.setBackgroundColor(Color.GRAY);
				}
				
				square.setGravity(Gravity.CENTER);
				square.setWidth(square_WIDTH);
				square.setHeight(square_HEIGHT);

				square.setClickable(true);
				final Context context = this;

				final int final_y = y;
				final int final_x = x;
				square.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View arg0) {
						active_x = final_x;
						active_y = final_y;
						AlertDialog.Builder builder;
						final AlertDialog alertDialog;

						Context mContext = context;
						LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
						View layout = inflater.inflate(R.layout.custom_dialog,
								(ViewGroup) findViewById(R.alert.layout_root));


						TableLayout table = (TableLayout) layout.findViewById(R.alert.table);
						table.setBackgroundColor(Color.RED);
						TableRow row = new TableRow(context);
						row.setPadding(1, 1, 1, 1);
						row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

						for(int i=1; i<=9; i++) {
							TextView button = new TextView(context);
							button.setText(""+i);
							button.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
							button.setWidth(square_WIDTH);
							button.setHeight(square_HEIGHT);
							button.setBackgroundColor(Color.GRAY);

							final int number = i;
							button.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									setValue(number);
									drawSudoku();
									activeDialog.cancel();
								}

							});

							row.addView(button);

							if(i == 3 || i == 6) {
								table.addView(row);
								row = new TableRow(context);
								row.setPadding(1, 1, 1, 1);
								row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
							}
							else {
								TextView padding = new TextView(context);
								padding.setWidth(1);
								row.addView(padding);
							}
						}

						table.addView(row);


						builder = new AlertDialog.Builder(mContext);
						builder.setView(layout);
						alertDialog = builder.create();
						activeDialog = alertDialog;
						alertDialog.show();
					}

				});

				row.addView(square);
				row.setGravity(Gravity.CENTER);
			}
			table.addView(row);



		}
		// function buttons
		TableLayout buttonTable = new TableLayout(this);
		TableRow row = new TableRow(this);

		Button generate = new Button(this);
		generate.setText("Generate");
		generate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				clean();
				drawSudoku();
			}
			
		});
		row.addView(generate);

		Button solve = new Button(this);
		solve.setText("Solve");
		solve.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				sudoku.solve();
				drawSudoku();
			}
			
		});
		row.addView(solve);

		Button settings = new Button(this);
		settings.setText("Settings");
		row.addView(settings);
		row.setGravity(Gravity.CENTER);


		buttonTable.addView(row);
		buttonTable.setGravity(Gravity.CENTER);
		layout.addView(buttonTable);
		layout.setGravity(Gravity.CENTER);
	}
	
	private void clean() {
		this.sudoku = new Sudoku(SUDOKU_SIZE);
		enterSudoku();
	}
	
}